#include <iostream>
#include "basic/basic.h"
#include "thread/blocked_thread.hpp"
#include "thread/auto_thread.hpp"
#include "thread/scoped_thread.hpp"

#include "thread/thread.h"
#include "thread/thread_pool/thread_pool.h"

#include "process/processor.h"

#include <boost/foreach.hpp>
#include <boost/make_shared.hpp>

void test_chan(eyas::process::chan<int>& ch){
	std::cout << "send num:1,th:" << boost::this_thread::get_id() << std::endl;
	ch << 1;
	boost::this_thread::sleep(boost::posix_time::seconds(1));
	std::cout << "send num:2,th:" << boost::this_thread::get_id() << std::endl;
	ch << 2;
	boost::this_thread::sleep(boost::posix_time::seconds(3));
	std::cout << "send num:3,th:" << boost::this_thread::get_id() << std::endl;
	ch << 3;
}

class chan_value{
public:
	chan_value():id_(0){}
	chan_value(int id, const char* str):id_(id), str_(str){}
	chan_value(const chan_value& value):id_(value.id_), str_(value.str_){}
private:
	int id_;
	std::string str_;

public:
	chan_value& operator=(const chan_value& value){
		this->id_ = value.id_;
		this->str_ = value.str_;
		return *this;
	}
	void print(){
		std::cout << "chan_value::print id:" << this->id_ << ",str:" << this->str_ << std::endl;
	}
};
void test_chan_ex(eyas::process::chan<chan_value>& ch){
	std::cout << "send num:1,th:" << boost::this_thread::get_id() << std::endl;
	ch << chan_value(1, "first value");
	boost::this_thread::sleep(boost::posix_time::seconds(1));
	std::cout << "send num:2,th:" << boost::this_thread::get_id() << std::endl;
	ch << chan_value(2, "first value");
	boost::this_thread::sleep(boost::posix_time::seconds(3));
	std::cout << "send num:3,th:" << boost::this_thread::get_id() << std::endl;
	ch << chan_value(3, "first value");
}

int main() {
	{	// 测试互斥通道
		eyas::process::init();
		std::cout << "Enter test mutex chan" << std::endl;
		getchar();
		eyas::process::chan<int> ch = eyas::process::create_chan<int>();
		eyas::process::fly(&test_chan, ch);
		int num = 0;
		for(int i=0; i<3; i++){
			if(ch >> num){
				std::cout << "received num:" << num << ",th:" << boost::this_thread::get_id() << std::endl;
			}else{
				break;
			}
		}
		std::cout << "mutex chan base type test completed" << std::endl;

		std::cout << "test mutex chan custom object" << std::endl;
		eyas::process::chan<chan_value> ch_obj = eyas::process::create_chan<chan_value>();
		eyas::process::fly(&test_chan_ex, ch_obj);
		chan_value value;
		for(int i=0; i<3; i++){
			if(ch >> value){
				value.print();
			}else{
				break;
			}
		}
	}
	std::cout << "Enter exit the test." << std::endl;
	getchar();
	return 0;
}
